package com.googlecode.gwtbb.client.only.widget.form.entry;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.gwtbb.client.shared.util.ObjectUtils0;
import com.googlecode.gwtbb.client.shared.validation.FieldId;

import java.util.List;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Apr 30, 2008
 */
public class LabeledRadioGroupFormEntry extends LabeledFormEntry {
	private List<ValueRadioButton> entryRadioGroup;
	private Panel radioButtonsPanel;

	public LabeledRadioGroupFormEntry(FieldId fieldId, String defaultValue, int row, int col, String labelText, List<ValueRadioButton> entryRadioGroup) {
		super(fieldId, defaultValue, row, col, labelText);
		this.entryRadioGroup = entryRadioGroup;
		radioButtonsPanel = new FlowPanel();
		for (ValueRadioButton vbr : entryRadioGroup)
			radioButtonsPanel.add(vbr);
		clearValue();
	}

	public List<ValueRadioButton> getEntryRadioGroup() {
		return entryRadioGroup;
	}

	public Panel getRadioButtonsPanel() {
		return radioButtonsPanel;
	}

	public Widget getEntryWidget() {
		return radioButtonsPanel;
	}

	public String getValue() {
		for (ValueRadioButton vbr : entryRadioGroup)
			if (vbr.getValue())
				return vbr.getInnerValue();
		return null;
	}

	public void setValue(String value) {
		for (ValueRadioButton vbr : entryRadioGroup)
			vbr.setValue(ObjectUtils0.nullSafeEquals(vbr.getInnerValue(), value));
	}
}
